﻿using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace FLH.RegionDataValues
{
	/// <summary>
	/// Provides the IQuerable object of the AzureDataModel. This is used in creating the
	/// Azure tables and to pass type information to the application about the data model.
	/// </summary>
	public class RegionDataValuesServiceContext : TableServiceContext, IAzureSettings
	{
		public RegionDataValuesServiceContext(string baseAddress, StorageCredentials credentials)
			: base(baseAddress, credentials)
		{
		}

		// The properties below are implemented here so that the tablename and DataConnectionString can
		// be modified through the setting of the properties.  The defaults should be set in the configuration
		// file.  These values are picked up in the call to the table initialize on application start.
		// 
		// In production code where the table names are known and the dataconnections are fixed then these
		// properties should be removed. SEE AzureTableInit.cs for more information.

		/// <summary>
		/// AzureTableDataConnectionString Property
		/// This is the connection string used int the service configuration it MUST match your configuration.
		/// </summary>
		public string AzureTableDataConnectionString
		{
			get { return AzureTableInitialization.AzureTableDataConnectionString; }	//the backing field is in AzureTableInitialization class
			set { AzureTableInitialization.AzureTableDataConnectionString = value; }
		}

		/// <summary>
		/// The table name to be created. 
		/// </summary>
		public string DataTableName
		{
			get { return AzureTableInitialization.DataTableName; }			//the backing field is in AzureTableInitialization class
			set { AzureTableInitialization.DataTableName = value; }
		}


		/// <summary>
		/// Gets the data model object. 
		/// </summary>
		public IQueryable<RegionDataValuesModel> DataTable
		{
			get
			{
                return CreateQuery<RegionDataValuesModel>(AzureTableInitialization.DataTableName);
			}
		}
	}
}


